package com.know.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;

import org.apache.commons.lang.StringUtils;

import com.know.entity.Friends_links;
import com.know.service.ImageService;
import com.know.service.LinksService;
import com.kzw.bean.Record;
import com.kzw.dao.support.Page;
import com.kzw.json.Jackson;

import my.mvc.RequestContext;
import my.util.ResponseUtils;

public class LinksAction {

	private LinksService linksService=new LinksService();
	private ImageService imageService=new ImageService();
	/**
	 * 读取友情链接列表
	 * @param rc
	 */
	public void getlinks(RequestContext rc)throws  ServletException, IOException{
		List<Friends_links> list=new ArrayList<>();
		list=linksService.list();
		String json=Jackson.me().toJson(list);
		ResponseUtils.renderJson(rc.response(), json);
	}
	/**
	 * 表格加载列表
	 * */
	public void talist(RequestContext rc) throws ServletException, IOException {
		int pageNo = rc.param("page", 1);
		int pageSize = rc.param("limit", 5);
		String orderBy = rc.param("orderBy", "");
		String orderDir = rc.param("orderDir", "");
		
		Page<Friends_links> page = new Page<>(pageNo, pageSize);
		if(StringUtils.isNotBlank(orderBy)) {
			page.setOrderBy(orderBy);
			page.setOrder(orderDir);
		}
		
		Record record = linksService.findPage(page);
		// 输出JSON
		String json = Jackson.me().toJson(record);
		//System.out.println(json);
		ResponseUtils.renderJson(rc.response(), json);
	}
	
		/**
	 * 根据链接ID读取（用于编辑）
	 */
	public void getone(RequestContext rc) throws ServletException,IOException{
		String id=rc.param("id");
		if(id!=null && !id.equals("")) {
			Friends_links friends_links = linksService.findById(Long.parseLong(id));
			rc.reqAttr("friends_links", friends_links);
			rc.forward("/admin/friendlink_form.jsp");
		}
	
	}
	/**
	 * 保存链接
	 */
	public void save(RequestContext rc) throws ServletException,IOException{
		// 将请求参数自动包装成Bean对象
		Friends_links fl = rc.form(Friends_links.class);
		String uuid=rc.param("imgid");
		String []uuids=null;
		if(uuid!=null)
			uuids=uuid.split(",");
		else
			fl.setFriend_link_logo(rc.param("friend_linksw"));
			
		if(fl.getFriend_link_id()==0) {
			// 增加
			long aid=linksService.save(fl);
			if(uuids!=null)
				imageService.update(uuids, aid);
		} else {
			// 修改
			linksService.update(fl);
			if(uuids!=null)
				imageService.update(uuids, fl.getFriend_link_id());

		}
		
		// 输出JSON
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
		
	}
	/**
	 * 链接删除
	 * @throws IOException
	 */
	public void del(RequestContext rc) throws ServletException,IOException{
		long id = rc.param("id", 0L);
		linksService.del(id);
		
		// 返回JSON
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	
	}
	/**
	 * 批量删除
	 * */
	public void multiDel(RequestContext rc) throws ServletException, IOException {
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			linksService.multiDel(ids);
		}		
		String json = Jackson.me().toJson(new Record(true));
		ResponseUtils.renderJson(rc.response(), json);
	}
	
	
}
